package com.legna.mhxy.support.service;

import com.legna.mhxy.support.common.SupportException;
import com.legna.mhxy.support.dao.RingCategoryDao;
import com.legna.mhxy.support.dao.RingTypeDao;
import com.legna.mhxy.support.dao.UserRingDao;
import com.legna.mhxy.support.entity.RingCategory;
import com.legna.mhxy.support.entity.RingType;
import com.legna.mhxy.support.entity.UserRing;
import com.legna.mhxy.support.utils.ExcelUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@Service
@Slf4j
@AllArgsConstructor
public class ImportService {

    private final RingCategoryDao ringCategoryDao;
    private final RingTypeDao ringTypeDao;
    private final UserRingDao userRingDao;

    @Transactional(rollbackFor = Exception.class)
    public void importExcel(MultipartFile file) throws IOException {
        String name = file.getName();
        Long userId = 4L;
        String sourceName = file.getOriginalFilename();
        InputStream fileInput = file.getInputStream();
        // 解析excel
        // Excel文件
        Workbook book;
        if (ExcelUtils.isExcel2007(sourceName)) {
            book = new XSSFWorkbook(fileInput);
        } else {
            book = new HSSFWorkbook(fileInput);
        }
        // 得到第一个sheet
        Sheet sheet = book.getSheetAt(0);
        // 得到Excel的行数
        int totalRows = sheet.getPhysicalNumberOfRows();

        for (int i = 1; i < totalRows; i++) {
            Row row = sheet.getRow(i);
            Cell categoryCell = row.getCell(0);
            RingCategory ringCategory = ringCategoryDao.selectCategoryByCode(categoryCell.getStringCellValue());
            if (ringCategory == null){
                throw new SupportException("环类型不存在");
            }

            Cell type60Cell = row.getCell(1);
            RingType ring60Type = getRingType(type60Cell.getStringCellValue());

            Cell type70Cell = row.getCell(3);
            RingType ring70Type = getRingType(type70Cell.getStringCellValue());

            Cell type80Cell = row.getCell(5);
            RingType ring80Type = getRingType(type80Cell.getStringCellValue());


            Cell type60CountCell = row.getCell(2);
            Double type60Count = type60CountCell.getNumericCellValue();
            insertUserRing(ring60Type,userId,type60Count);

            Cell type70CountCell = row.getCell(4);
            Double type70Count = type70CountCell.getNumericCellValue();
            insertUserRing(ring70Type,userId,type70Count);

            Cell type80CountCell = row.getCell(6);
            Double type80Count = type80CountCell.getNumericCellValue();
            insertUserRing(ring80Type,userId,type80Count);
        }
    }

    public RingType getRingType(String typeCode){
        RingType ringType = ringTypeDao.selectTypeByCode(typeCode);
        if (ringType == null){
            throw new SupportException("环不存在");
        }
        return ringType;
    }
    public void insertUserRing(RingType ringType,Long userId,Double ringCount){
        UserRing userRing = userRingDao.selectUserRing(ringType.getId(),userId);
        if (userRing == null){
            userRing = new UserRing(ringType.getId(),userId,ringCount);
            userRingDao.insert(userRing);
        }else{
            userRingDao.updateById(userRing);
        }
    }
}
